package com.frame.pageform;

import java.util.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;

public class RequestContext {

	/**
	 * request
	 */
	private HttpServletRequest request;

	/**
	 * response
	 */
	private HttpServletResponse response;
	private HttpServlet servlet;
	private PageForm pageForm;
	private PageOgnlContext pageOgnlContext;
	private Map<String,List> parameterMap;
	private List<FileItem> FileItemList;
	/**
	 * 保存线程中的环境
	 */
	static private ThreadLocal local = new ThreadLocal();

	public RequestContext(HttpServletRequest request,HttpServletResponse response,HttpServlet servlet){
		this.request=request;
		this.response=response;
		this.servlet=servlet;
		local.set(this);
	}
	static public RequestContext getContext(){
		return (RequestContext)local.get();
	}
	static public void clear(){
		local.set(null);
	}

	public Object executeExpression(String expression) throws Exception
	{
		return PageBeanUtil.getOgnlValue(expression, pageForm, getPageOgnlContext().createtext());
	}

	public String getRealPath(String root){
		return servlet.getServletContext().getRealPath(root);
	}
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	public HttpServlet getServlet() {
		return servlet;
	}
	public void setServlet(HttpServlet servlet) {
		this.servlet = servlet;
	}
	public PageForm getPageForm() {
		return pageForm;
	}
	public void setPageForm(PageForm pageForm) {
		this.pageForm = pageForm;
	}
	public PageOgnlContext getPageOgnlContext() {
		if(pageOgnlContext==null)
			pageOgnlContext=new PageOgnlContext();
		return pageOgnlContext;
	}
	public void setPageOgnlContext(PageOgnlContext pageOgnlContext) {
		this.pageOgnlContext = pageOgnlContext;
	}
	public Map<String,List> getParameterMap() {
		return parameterMap;
	}
	public void setParameterMap(Map<String,List> parameterMap) {
		this.parameterMap = parameterMap;
	}
	public List<FileItem> getFileItemList() {
		return FileItemList;
	}
	public void setFileItemList(List<FileItem> fileItemList) {
		FileItemList = fileItemList;
	}

}
